﻿package com.everydayflash.demos
{
	import com.everydayflash.util.Rotator;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.events.KeyboardEvent;
	import flash.net.URLRequest;
	
	/**
	 * Use it as the document class to run the demo.
	 * 
	 * @see com.everydayflash.util.Rotator for more info
	 * 
	 * @author Bartek Drozdz (http://www.everydayflash.com)
	 * @version 1.0
	 */
	public class RotatorDemo extends Sprite {
		
		private var pointer:Bitmap;
		private var rotated:Sprite;
		private var rotator:Rotator;
		private var speed:Number = 6;
		private var square:Boolean = false;
		
		public function RotatorDemo():void {
			mouseChildren = false;
			
			var bg:Loader = new Loader();
			bg.load(new URLRequest("bg.jpg"));
			addChild(bg);
			
			rotated = new Sprite();
			rotated.x = stage.stageWidth / 2;
			rotated.y = stage.stageHeight / 2;
			
			drawRotated();
			addChild(rotated);
			
			rotated.filters = [
				new BevelFilter(10, 45, 0xffffff, .7, 0x000000, .7, 13, 13, 1, 3), 
				new DropShadowFilter(8, 45, 0, 1, 8, 8, .5, 3)
			];
			
			rotator = new Rotator(rotated, new Point(stage.stageWidth / 2, stage.stageHeight / 2));
			
			var pointerData:BitmapData = new BitmapData(5, 5, true, 0x00000000);
			pointerData.fillRect(new Rectangle(2, 0, 1, 5), 0xffffffff);
			pointerData.fillRect(new Rectangle(0, 2, 5, 1), 0xffffffff);
			pointer = new Bitmap(pointerData);
			pointer.x = stage.stageWidth / 2 - 3;
			pointer.y = stage.stageHeight / 2 - 3;
			
			addChild(pointer);

			stage.addEventListener(MouseEvent.CLICK, moveRegistrationPoint);
			stage.addEventListener(KeyboardEvent.KEY_UP, changeDirection);
		}
		
		private function drawRotated(ns:Boolean = false):void {
			square = ns;
			var g:Graphics = rotated.graphics;
			g.clear();
			
			var checker:int = 6;
			var b:BitmapData;
			
			switch(square) {
				case true:
					b = new BitmapData(checker*2, checker*2, false, 0xff660000);
					b.fillRect(new Rectangle(0, 0, checker, checker), 0xffdd0000);
					b.fillRect(new Rectangle(checker, checker, checker, checker), 0xffdd0000);
					g.beginBitmapFill(b, null, true, true);
					g.drawRect(-20, -20, 40, 40);
					g.endFill();
					break;
				case false:
					b = new BitmapData(checker*2, checker*2, false, 0xff006600);
					b.fillRect(new Rectangle(0, 0, checker, checker), 0xff00dd00);
					b.fillRect(new Rectangle(checker, checker, checker, checker), 0xff00dd00);
					g.beginBitmapFill(b, null, true, true);
					g.drawCircle(0, 0, 21);
					g.endFill();
					break;
			} 
		}
		
		private function changeDirection(ke:KeyboardEvent):void {
			if (ke.keyCode == 32) drawRotated(!square);
			else speed *= -1;
		}
		
		private function rotate(e:Event):void {
			rotator.rotation += speed;
		}

		private function moveRegistrationPoint(me:MouseEvent):void {
			if (!hasEventListener(Event.ENTER_FRAME)) {
				addEventListener(Event.ENTER_FRAME, rotate);
			} else {
				rotator.setRegistrationPoint(new Point(me.stageX, me.stageY));
				pointer.x = me.stageX - 3;
				pointer.y = me.stageY - 3;
			}
		}
	}
}















